/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;
using System.Xml;
using System.Text.RegularExpressions;

namespace DaveSexton.DocProject.Sandcastle.ContentManagement
{
	[Serializable]
	public class DivContentItem : XmlContentItem
	{
		#region Public Properties
		public override string Value
		{
			get
			{
				string value = base.Value;
				Match match = divWrapperRegex.Match(value);

				if (match.Success)
					return match.Groups["innerXml"].Value;
				else
					return value;
			}
			set
			{
				base.Value = string.Format(System.Globalization.CultureInfo.InvariantCulture, divWrapper, value);
			}
		}
		#endregion

		#region Private / Protected
		private readonly string divWrapper;
		private readonly Regex divWrapperRegex;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="DivContentItem" /> class.
		/// </summary>
		public DivContentItem(IXPathNavigable node, string name, bool designable)
			: base(node, name, designable)
		{
			divWrapper = @"<div class=""" + name + @""">{0}</div>";
			divWrapperRegex = new Regex(@"^\s*<div\s+class=""" + name + @"""(>(?<innerXml>.*)</div>|\s*/>)\s*$",
				RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant);
		}
		#endregion
	}
}
